/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "gl_local.h"

/*-----------------------------------------------------------------------------
	GL FX manager :
-----------------------------------------------------------------------------*/

//
//	EGLEffect::EGLEffect
//
EGLEffect::EGLEffect( EGLFXManager *fxm, CGeffect effect )
{
	this->fxm		=	fxm;
	this->effect	=	effect;
	current_tech	=	current_tech;	
	current_tech_passes.reserve(8);
}


//
//	EGLEffect::~EGLEffect
//
EGLEffect::~EGLEffect( void )
{
	if (effect && cgIsEffect(effect) ) {
		cgDestroyEffect( effect );
	}
}


//
//	EGLEffect::SetTechnique
//
void EGLEffect::SetTechnique( const EFXHandle tech )
{
	current_tech	=	(CGtechnique)tech;
	
	if (!cgValidateTechnique( current_tech )) {
		LOG_WARNING("Technique is not valid");
	}
}


//
//	EGLEffect::SetTechnique
//
void EGLEffect::SetTechnique( const char* tech )
{
	SetTechnique( GetTechniqueByName(tech) );
}


//
//	EGLEffect::Begin
//
void EGLEffect::Begin( uint &num_passes )
{
	CGpass  pass = cgGetFirstPass( current_tech );
	
	while ( pass ) {
		current_tech_passes.push_back( pass);
		pass = cgGetNextPass(pass);
	}
	
	num_passes	=	current_tech_passes.size();
}


//
//	EGLEffect::BeginPass
//
void EGLEffect::BeginPass( uint pass )
{
	current_pass	=	current_tech_passes[ pass ];
	
	cgSetPassState( current_pass );
}


//
//	EGLEffect::EndPass
//
void EGLEffect::EndPass( void )
{
	cgResetPassState( current_pass );
}


//
//	EGLEffect::End
//
void EGLEffect::End( void )
{
	current_tech_passes.clear();
}


//
//
//
void EGLEffect::CommitChanges( void )
{
	//	TODO : Does OpenGL Cg environment it automatically?
}


//
//	EGLEffect::GetParameterByName
//
EFXHandle EGLEffect::GetParameterByName( EFXHandle param, const char *name )
{
	CGparameter	cgparam	=	cgGetNamedEffectParameter( effect, name );
	return (EFXHandle)cgparam;
}


//
//	EGLEffect::GetTechniqueByName
//
EFXHandle EGLEffect::GetTechniqueByName( const char *name )
{
	CGtechnique	tech	=	cgGetNamedTechnique( effect, name );
	return (EFXHandle)tech;
}


/*-----------------------------------------------------------------------------
	Set functions :	
-----------------------------------------------------------------------------*/

void	EGLEffect::SetUniform		( const char* param, bool b )							{ SetUniform( GPN(param), b );	}
void	EGLEffect::SetUniform		( const char* param, int i )							{ SetUniform( GPN(param), i );	}	
void	EGLEffect::SetUniform		( const char* param, uint u )							{ SetUniform( GPN(param), u );	}
void	EGLEffect::SetUniform		( const char* param, float f )							{ SetUniform( GPN(param), f );	}
void	EGLEffect::SetUniform		( const char* param, const EVector		&v )			{ SetUniform( GPN(param), v );	}
void	EGLEffect::SetUniform		( const char* param, const EPoint		&p )			{ SetUniform( GPN(param), p );	}
void	EGLEffect::SetUniform		( const char* param, const EQuaternion	&q )			{ SetUniform( GPN(param), q );	}
void	EGLEffect::SetUniform		( const char* param, const EColor		&c )			{ SetUniform( GPN(param), c );	}
void	EGLEffect::SetUniform		( const char* param, const EMatrix &m )					{ SetUniform( GPN(param), m );	}
void	EGLEffect::SetUniformArray	( const char* param, const EVector *v, uint count )		{ SetUniformArray( GPN(param), v, count );	}
void	EGLEffect::SetUniformArray	( const char* param, const EMatrix *m, uint count )		{ SetUniformArray( GPN(param), m, count );	}
void	EGLEffect::SetTexture		( const char* param, const IPxTexture &t )				{ SetTexture( GPN(param), t );	}

#define CHECK_CG_PARAM(param)	if (!cgIsParameter((CGparameter)param)) return;

//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, bool b )
{
	CHECK_CG_PARAM( param );
	
	if (b) {
		cgSetParameter1i( (CGparameter)param, 1 );
	} else {
		cgSetParameter1i( (CGparameter)param, 0 );
	}
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, int i )
{
	CHECK_CG_PARAM( param );
	cgSetParameter1i( (CGparameter)param, i );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, uint u )
{
	CHECK_CG_PARAM( param );
	cgSetParameter1i( (CGparameter)param, u );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, float f )
{
	CHECK_CG_PARAM( param );
	cgSetParameter1f( (CGparameter)param, f );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, const EVector &v )
{
	CHECK_CG_PARAM( param );
	cgSetParameter4fv( (CGparameter)param, v.Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, const EPoint &p )
{
	CHECK_CG_PARAM( param );
	cgSetParameter4fv( (CGparameter)param, p.Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, const EQuaternion &q )
{
	CHECK_CG_PARAM( param );
	cgSetParameter4fv( (CGparameter)param, q.Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, const EColor &c )
{
	CHECK_CG_PARAM( param );
	cgSetParameter4fv( (CGparameter)param, c.Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniform( const EFXHandle param, const EMatrix &m )
{
	CHECK_CG_PARAM( param );
	cgGLSetMatrixParameterfr( (CGparameter)param, m.Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniformArray( const EFXHandle param, const EVector *v, uint count )
{
	CHECK_CG_PARAM( param );
	cgGLSetParameterArray4f( (CGparameter)param, 0, count, v->Ptr() );
}


//
//	EGLEffect::SetUniform
//
void EGLEffect::SetUniformArray( const EFXHandle param, const EMatrix *m, uint count )
{
	CHECK_CG_PARAM( param );
	cgGLSetMatrixParameterArrayfr( (CGparameter)param, 0, count, m->Ptr() );
}


//
//	EGLEffect::SetTexture
//
void EGLEffect::SetTexture( const EFXHandle param, const IPxTexture &t )
{
	CHECK_CG_PARAM( param );

	const EGLBaseTexture *gl_texture	=	t.as<const EGLBaseTexture>();

	GLuint	texobj	=	gl_texture->GetTexObject();
	
	cgSetSamplerState( (CGparameter)param );
	cgGLSetTextureParameter( (CGparameter)param, texobj );
	cgGLSetupSampler( (CGparameter)param, texobj );
}